package com.zyl.basis.state;

/**
 * @author zhaoyl
 * @date 2024/5/27 16:09
 * @description 说明
 *线程调度器会优先调用优先级高的就绪线程（但不一定会先执行）
 * 优先级 1- 10
 */
public class TestPriority {
    //main主线程默认也是5
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority());
        MyPriority myPriority = new MyPriority();
        Thread t1 = new Thread(myPriority,"线程1");
        Thread t2 = new Thread(myPriority,"线程2");
        Thread t3 = new Thread(myPriority,"线程3");
        Thread t4 = new Thread(myPriority,"线程4");
        Thread t5 = new Thread(myPriority,"线程5");

        t1.start();//默认5
        //必须先设置在启动，不然默认5

        t2.setPriority(1);//概率最低
        t2.start();

        t3.setPriority(4);
        t3.start();

        t4.setPriority(Thread.MAX_PRIORITY);// 10
        t4.start();

        t5.setPriority(8);
        t5.start();

    }
}

class MyPriority implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority());
    }
}

